layui.config({
    base: "../static/expand/tree_table/"
}).define(['layer', 'table', 'element', 'form', 'jquery' , 'laydate' , 'treeTable' ], function (exports) {
    // noinspection SpellCheckingInspection
    var layer = layui.layer //弹层
        , table = layui.table //表格
        , element = layui.element //元素操作
        , $ = layui.jquery
        ,treeTable = layui.treeTable
        , form = layui.form
        , laydate = layui.laydate;

    var birthday_date = null;
    var form_province  ;
    var form_city  ;
    var form_county ;
    var	re;
    jobPage = {



        //职位树形表格初始化
        init_job_tree_table: function(){
            re = treeTable.render({
                elem: '#job_tree_table',
                icon_key: 'jobName',
                primary_key: 'jobId',
                parent_key: 'parentId',
                url: '/manage/job/findJobAll',
                is_checkbox: true,
                checked: {
                    key: 'jobId',
                    // data: [0,1,4,10,11,5,2,6,7,3,8,9],
                },
                end: function(e){
                    form.render();
                },
                cols: [
                    {
                        key: 'jobName',
                        title: '名称',
                        width: '100px',

                    },
                    {
                        key: 'jobId',
                        title: 'jobId',
                        width: '100px',
                        align: 'center',
                    },
                    {
                        key: 'parentId',
                        title: '父ID',
                        width: '100px',
                        align: 'center',
                    },
                     {key: 'averageSalary', title: '职称基础薪水', align: 'center', sort: true, fixed: 'left', template: function(item){
                         var averageSalary = item.averageSalary;
                         if (averageSalary!=undefined){
                             if (averageSalary===-1){
                                 return "/";
                             }else {
                                 return averageSalary;
                             }
                         }else{
                             return "/";
                         }
                         }},
                     {key: 'createTime', title: '创建时间', align: 'center', sort: true, fixed: 'left'},
                     {key: 'updateTime', title: '更新时间', align: 'center', sort: true, fixed: 'left',  template: function(item) {
                            var updateTime = item.updateTime;
                            if (updateTime!=undefined){
                                return updateTime;
                            } else {
                                return "/";
                            }
                         },
                     },
                    {
                        key: 'status',
                        title: '状态',
                        width: '100px',
                        align: 'center',
                        template: function(item){
                            var status = item.status;
                            if (status===1){
                                return '<input type="checkbox"  name="close" lay-skin="switch" lay-text="启用|禁用" checked> ';
                            } else {
                                return '<input type="checkbox" name="close" lay-skin="switch" lay-text="启用|禁用"> ';
                            }

                        }
                    },
                    {
                        title: '操作',
                        align: 'center',
                        template: function(item) {
                            if (item.type === 0) {
                                return '<a lay-filter="add_job">添加</a> |' +
                                    ' <a lay-filter="edit_job"">编辑</a> |' +
                                    ' <a lay-filter="deleted_job">删除</a>';
                            } else {
                                return ' <a lay-filter="edit_job"">编辑</a> |' +
                                    ' <a lay-filter="deleted_job">删除</a>';
                            }
                        },
                    }
                ]
            });
        },

        //监听表格行工具事件   tool是行工具条
        init_listen_table_row_add() {
            treeTable.on('tree(add_job)', function (data) { //注：tool 是工具条事件名，demo 是 table 原始容器的属性 lay-filter="对应的值"
                data = data.item;
                jobPage.init_job_form('add', '',data);
            });
            treeTable.on('tree(edit_job)', function (data) { //注：tool 是工具条事件名，demo 是 table 原始容器的属性 lay-filter="对应的值"
                data = data.item;
                jobPage.init_job_form('edit', data,"");
            });
            treeTable.on('tree(deleted_job)', function (data) { //注：tool 是工具条事件名，demo 是 table 原始容器的属性 lay-filter="对应的值"
                data = data.item;
                jobPage.delete_job(data);
            });
        },

        //监听表格头工具条事件 toolbar是头工具条
        init_listen_table_toolbar() {
            $("#add_top_job").click(function () {

                jobPage.init_job_form('add_top', '','');
            });

            $("#deleted_top_job").click(function () {

                 var chooseId = treeTable.checked(re).join(',');
                jobPage.batch_delete_job(chooseId);
            });
        },

        //打开新增/编辑弹框，这一层主要负责是否给编辑框赋值--->打开弹出层参数 type 类型 ,data 编辑改行的数据
        init_job_form: function (type, data ,d) {
            var form_title = '';
            type === 'add' ? form_title = "添加职位基本信息" : form_title = "编辑职位基本信息";
            $.get('/manage/form/edit_job.html', function (html) {
                nowIndex = layer.open(  //弹出层
                    {
                        type: 1,
                        title: form_title,
                        area: ['900px', '450px'],
                        btnAlign: 'c',//按钮居中对齐
                        // content: $('#edit_job_form'),
                        content: html,
                        anim: 5,//弹出效果 渐显
                        shift: -1,
                        btn: ['保存', '关闭'],
                        yes: function (index, layero) {
                            form.on('submit(submit_job)',function(){
                                jobPage.update_form(type, data);
                                return false;
                            });


                        },
                        btn2: function () {
                        },
                        success: function (layero,index) {

                            if (type === 'edit') {

                                $("#jobName").val(data.jobName);
                                $("#jobRank").val(data.jobRank);
                                $("#averageSalary").val(data.averageSalary);
                                $("#info").val(data.info);
                                var status = data.status;
                                status === 1 ? status = true :status = false;
                                $("#status").prop("checked",status);
                                jobPage.choose_radio_form(type,data);
                                form.render();
                            } else  if (type === 'add_top') {
                                jobPage.choose_radio_form('add','','');
                            }else {
                                jobPage.choose_radio_form('add_row',d);
                            }

                            layero.addClass('layui-form');//添加form标识
                            layero.find('.layui-layer-btn0').attr('lay-filter','submit_job').attr('lay-submit','');//将按钮弄成能提交的
                            form.render();

                        },

                    });
            })
        },



        //表单新增/编辑  新增就提交表单，编辑就赋值表单等待表单提交
        update_form: function (type, data) {

            var averageSalary = $("#averageSalary").val();
            var jobRank = parseInt($("#jobRank").val());
            var form_type  = parseInt($("#job_type input[name=type]:checked ").val());
            var parentId = $("#search_jop_parent").val();
            //如果类型是父级，把薪水和等级清空
            if(form_type===0){
                averageSalary  = null;
                jobRank = null;
            }
            // 请求方式处理和URL处理
            if (type === 'add' || type ==='add_top') {
                request_method = 'POST';
                request_url = '/manage/job';


            } else {
                request_method = 'PUT';
                request_url = '/manage/job/' + data.jobId;
            }
            var status = $("#status").prop("checked");
            status ? status = 1 : status = 0;

            var job_form_data = {
                jobName: $("#jobName").val(),
                jobRank: jobRank,
                averageSalary: averageSalary,
                type : form_type,
                parentId: parentId,
                info: $("#info").val(),
                status: status,
            };


            $.ajax({
                url: request_url,
                contentType: "application/json",
                type: request_method,
                data: JSON.stringify(job_form_data,),

                success: function (resp) {
                    if (resp.code === 200) {
                        var data = resp.data;

                        layer.msg('保存成功', {icon: 1});
                        layer.close(nowIndex);
                        jobPage.init_job_tree_table();
                    } else {
                        layui.form.render();
                        layer.msg("保存失败" + " " + resp.msg, {icon: 5});
                    }

                }
            });


        },

        //职位单行删除
        delete_job: function (data) {
            layer.confirm("是否删除[" + data.jobName + "]", function (index) {
                $.ajax({
                    type: "DELETE",
                    contentType: "application/json",//设置请求参数类型为json字符串
                    url: "/manage/job/" + data.jobId,
                    success: function (resp) {
                        var code = resp.code;
                        if (code === 200) {
                            layer.msg('删除成功', {icon: 1});
                            jobPage.init_job_tree_table();
                        } else {
                            layer.msg("删除失败 " + resp.msg, {icon: 5});
                        }
                    }
                });
                layer.close(index);
                //向服务端发送删除指令
            });
        },

        //职位批量删除 choose_data选中的行数据
        batch_delete_job: function (choose_data) {
            if (choose_data === undefined) {
                layer.msg('请选择一行');
            } else {

                layer.confirm('是否确定删除选中的数据[' + choose_data + "]",
                    {
                        icon: 0,
                        title: "提示",
                        skin: "deleteTips"
                    }
                    , function () {
                        $.ajax({
                            type: "POST",
                            contentType: "application/x-www-form-urlencoded",//设置请求参数类型为json字符串
                            url: "/manage/job/batchJob",
                            data: {
                                ids: choose_data,
                            },
                            success: function (resp) {
                                var code = resp.code;
                                if (code === 200) {
                                    layer.msg('删除成功', {icon: 1});
                                    jobPage.init_job_tree_table();
                                } else {
                                    layer.msg("删除失败", {icon: 5});
                                }
                            }
                        });


                    });
            }
        },



        // 表格页面监听表单中的switch改变发送请求
        listen_job_table_switch: function() {
            //监听行单击事件
            form.on('switch(job_table_Switch)', function(data){
                var switch_status = data.elem.checked?1:0;
                var row_data = JSON.parse( data.elem.getAttribute('row-data'));
                $.ajax({
                    url: "/manage/job/" + row_data.id,
                    contentType : "application/json",
                    type: 'PUT',
                    data:JSON.stringify( {status:switch_status}),
                    success: function (resp) {
                        if (resp.code===200){
                            var data = resp.data;
                            layer.msg('保存成功', {icon: 1});
                            jobPage.init_job_tree_table();
                        }else {
                            layui.form.render();
                            layer.msg("保存失败"+ " "+ resp.msg, {icon: 5});
                        }

                    }
                });

            });



        },


        // 编辑表格展示内容
        //       功能1 添加时候选取添加权限类型   显示表单
        choose_radio_form:function(type,row){

            if (type==="add"){
                form.on('radio(edit_type)', function(data){
                    if(data.value === "0"){ //0父级菜单
                        $("#edit_averageSalary").hide();
                        $("#edit_jobRank").hide();
                        $("#job_parent").hide();
                    }else{
                        $("#edit_averageSalary").show();
                        $("#edit_jobRank").show();
                        $("#job_parent").show();
                    }

                });
            } else if (type === 'add_row') {
                $("input[name=type][value='1']").attr("checked", true);
                $('#search_jop_parent').append(new Option(row.jobName,row.jobId,true,true));
                $("#edit_averageSalary").show();
                $("#edit_jobRank").show();
                $("#job_parent").show();



            }
            else {
                var t = row.type;

                $("input[name=type][value='0']").attr("checked", t === 0 ? true: false);
                $("input[name=type][value='1']").attr("checked", t === 1 ? true : false);

                if(t === 0 ){ //0父级菜单
                    $("#edit_averageSalary").hide();
                    $("#edit_jobRank").hide();
                    $("#job_parent").hide();
                }else{
                    $("#edit_averageSalary").show();
                    $("#edit_jobRank").show();
                    $("#job_parent").show();
                }


            }



        },

        //监听常规的单选框变化
        listen_job_choose_radio_form:function () {

            form.on('radio(edit_type)', function(data){

                if(data.value === "0"){ //0父级菜单
                    $("#edit_averageSalary").hide();
                    $("#edit_jobRank").hide();
                    $("#job_parent").hide();
                }else{
                    $("#edit_averageSalary").show();
                    $("#edit_jobRank").show();
                    $("#job_parent").show();
                }

            });
            form.render();
        }

    };

    // jobPage.init_job_table();
    jobPage.init_job_tree_table();
    jobPage.listen_job_table_switch();
    jobPage.init_listen_table_row_add();
    jobPage.init_listen_table_toolbar();
    jobPage.listen_job_choose_radio_form();
    exports("jobPage", jobPage);


});